﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using b7.Models;

namespace b7.Controllers
{
    [Authorize(Roles = "adminshop,admin")]
    public class BaiVietController : Controller
    {
        private b7Context db = new b7Context();

        //
        // GET: /BaiViet/

        public ViewResult Index()
        {
            var baiviet = db.BaiViets.Include(b => b.Member);
            //var baiviet = db.BaiViets;
            return View(baiviet.ToList());
        }

        //
        // GET: /BaiViet/Details/5

        public ViewResult Details(int id)
        {
            BaiViet baiviet = db.BaiViets.Find(id);
            return View(baiviet);
        }

        //
        // GET: /BaiViet/Create

        public ActionResult Create()
        {
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName");
            return View();
        } 

        //
        // POST: /BaiViet/Create

        [HttpPost]
        public ActionResult Create(BaiViet baiviet)
        {
            if (ModelState.IsValid)
            {
                db.BaiViets.Add(baiviet);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName", baiviet.MemberId);
            return View(baiviet);
        }
        
        //
        // GET: /BaiViet/Edit/5
 
        public ActionResult Edit(int id)
        {
            BaiViet baiviet = db.BaiViets.Find(id);
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName", baiviet.MemberId);
            return View(baiviet);
        }

        //
        // POST: /BaiViet/Edit/5

        [HttpPost]
        public ActionResult Edit(BaiViet baiviet)
        {
            if (ModelState.IsValid)
            {
                db.Entry(baiviet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MemberId = new SelectList(db.Members, "MemberId", "UserName", baiviet.MemberId);
            return View(baiviet);
        }

        //
        // GET: /BaiViet/Delete/5
 
        public ActionResult Delete(int id)
        {
            BaiViet baiviet = db.BaiViets.Find(id);
            return View(baiviet);
        }

        //
        // POST: /BaiViet/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            BaiViet baiviet = db.BaiViets.Find(id);
            db.BaiViets.Remove(baiviet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        //public ActionResult ViewNew()
        //{
        //    var baiviet = db.BaiViets;
        //    return View(baiviet.ToList());//day
        //    //nhung ma t lam 1 cai view cho no roi goi ben layout
        //    //cau thaychua. 
        //}
    }
}